package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.aop.annotation.LogAnno;
import com.qk.management.service.DeptService;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//部门管理控制层
@RestController
@RequestMapping("/depts")
//@SuppressWarnings("all")//告诉编译器忽略警告信息,提高性能
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/list")
    public Result findAllDepts(){
        List<Dept> list = deptService.getAllDepts();
        return Result.success(list);
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        deptService.deleteById(id);
        return Result.success();
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer deptId){
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);
    }

    @GetMapping
    public Result page(
            //@RequestParam(name="???",required = true/false,defaultValue = "???")
            //request默认为true,作用为是否必填
            //defaultValue为默认值
            @RequestParam(name="page",required = true,defaultValue = "1")Integer page,
            @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize,
            @RequestParam(name="name",required = false) String name,
            @RequestParam(name="status",required = false) Integer status){

        PageResult<Dept> pageResult = deptService.page(page, pageSize, name, status);
        System.out.println("DeptController  pageResult");
        System.out.println(pageResult);

        return Result.success(pageResult);
    }

    //新增部门接口开发  严格遵循接口文档
    //3步骤 : 1拉架子  2补全参数和返回值  3代码
    //@RequestMapping(value = "/depts",method = RequestMethod.POST)

    @LogAnno
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        System.out.println("DeptController  addDept");
        return Result.success();
    }
}
